3D Graphics Programming with QuickDraw 3D 1.5.4
Previous | QD3D Book | Overview | Chapter Contents | Next |
Picking IDs are particularly useful when picking in immediate mode. Listing 7 shows how to create a triangle, attach a picking ID to it, and then process hits.
Listing 7 Picking in immediate mode
void MyImmediateModePickID (TQ3ViewObject view, WindowPtr window)
{
TQ3WindowRectPickData myPickData;
TQ3TriangleData myTriangleData;
TQ3PickObject myPick;
TQ3ViewStatus myViewStatus;
unsigned long pickID;
Rect myPortRect;
Point myCenter;
unsigned long myNumHits;
/*Set up a triangle.*/
Q3Point3D_Set(&myTriangleData.vertices[0].point, -1.0, -0.5, 0.0);
Q3Point3D_Set(&myTriangleData.vertices[1].point, 1.0, 0.0, 0.0);
Q3Point3D_Set(&myTriangleData.vertices[2].point, -0.5, 1.5, 0.0);
myTriangleData.vertices[0].attributeSet = NULL;
myTriangleData.vertices[1].attributeSet = NULL;
myTriangleData.vertices[2].attributeSet = NULL;
myTriangleData.triangleAttributeSet = NULL;
/*Set up TQ3WindowPointPickData structure.*/
myPickData.data.sort = kQ3PickSortNone;
myPickData.data.mask = kQ3PickDetailMaskPickID | kQ3PickDetailMaskObject;
myPickData.data.numHitsToReturn = kQ3ReturnAllHits;
myPortRect = ((GrafPtr) window)->myPortRect;
myCenter.h = (myPortRect.right - myPortRect.left)/2.0;
myCenter.v = (myPortRect.bottom - myPortRect.top) /2.0;
Q3Point2D_Set(&myPickData.rect.min, myCenter.h - 5, myCenter.v - 5);
Q3Point2D_Set(&myPickData.rect.max, myCenter.h + 5, myCenter.v + 5);
/*Create the window rectangle window pick.*/
myPick = Q3WindowRectPick_New(&myPickData);
/*Submit the pick ID and triangle in immediate mode.*/
Q3View_StartPicking(view, myPick);
do
{
Q3PickIDStyle_Submit(kPickID, view);
Q3Triangle_Submit(&myTriangleData, view);
myViewStatus = Q3View_EndPicking(view);
} while (myViewStatus == kQ3ViewStatusRetraverse);
Q3Pick_GetNumHits(myPick, &myNumHits);
if (numHits == 1)
{
/*Get the pickID data (for first and only hit) and check if it's
the expected pick ID.*/
Q3Pick_GetPickDetailData(myPick, 0);
if (pickID == kPickID)
{
/*picked on triangle with pick ID*/
}
}
Q3Object_Dispose(myPick);
}
Previous | QD3D Book | Overview | Chapter Contents | Next |